package com.vst.test.generate.structure.springmvc.plugin.analizador;

import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CARPETA_DAO;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CARPETA_DOMINIO;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CARPETA_UTIL;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CLASS_DAO;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CLASS_ENTIDAD;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CLASS_IDAO;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.DONT;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.EXTENSION_JAVA;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.PACK_DAO_BASE;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.PACK_DAO_BASE_DOMINIO_UTIL;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.PACK_DAO_BASE_DOMINIO_UTIL_EXCEPTION;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.PACK_DAO_IMPL;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaModelException;

import com.vst.test.generate.structure.springmvc.plugin.util.EstructureObject;
import com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource;
import com.vst.test.generate.structure.springmvc.plugin.util.Util;

@SuppressWarnings("unused")
public class AnalizadorPaqueteDAO {

	private AnalizarrecursosInProject analizador;
	private AnalizadorEntidadesDominio analizadorEntidadesDominio;
	private EstructureObject estructureObjectGeneralProject = null;
	private String rutaPackageDAO = "";
	private String rutaPackageDAOBASE = "";
	private String rutaPackageDAOBaseDominioUtil = "";
	private String rutaPackageDAOBaseDominioUtilException = "";
	private String rutaPackageDAOIMPL = "";
	private List<ObjectCompilationUnit> listaObjectCompilationUnits ;

	private boolean existePackageDAO = false, existePackageDAOBASE = false, existePackageDAOBaseDominioUtil = false, existePackageDAOBaseDominioUtilExceptionL = false, existePackageDAOIMPL = false;
	
	public AnalizadorPaqueteDAO(AnalizarrecursosInProject analizador, AnalizadorEntidadesDominio analizadorEntidadesDominio) {
		this.analizador = analizador;
		this.analizadorEntidadesDominio = analizadorEntidadesDominio;
		this.estructureObjectGeneralProject = analizador.getEstructureObjectGeneralProject();
		listaObjectCompilationUnits = new ArrayList<ObjectCompilationUnit>();
	}

	public void scanPackageExists() throws JavaModelException {
		rutaPackageDAO = estructureObjectGeneralProject.getPackageBase() + DONT + CARPETA_DAO; // interfaz
		rutaPackageDAOBASE = estructureObjectGeneralProject.getPackageBase() + DONT + PACK_DAO_BASE; // clase
		rutaPackageDAOIMPL = estructureObjectGeneralProject.getPackageBase() + DONT + PACK_DAO_IMPL; // clase
		rutaPackageDAOBaseDominioUtil = estructureObjectGeneralProject.getPackageBase() + DONT + PACK_DAO_BASE_DOMINIO_UTIL; // clase
		rutaPackageDAOBaseDominioUtilException = estructureObjectGeneralProject.getPackageBase() + DONT + PACK_DAO_BASE_DOMINIO_UTIL_EXCEPTION; // clase
		existePackageDAO = scanRutaPackageDAO(rutaPackageDAO);
		existePackageDAOBASE = scanRutaPackageDAO(rutaPackageDAOBASE);
		existePackageDAOIMPL = scanRutaPackageDAO(rutaPackageDAOIMPL);
		existePackageDAOBaseDominioUtil =  scanRutaPackageDAO(rutaPackageDAOBaseDominioUtil);
		existePackageDAOBaseDominioUtilExceptionL =  scanRutaPackageDAO(rutaPackageDAOBaseDominioUtilException);
	}

	private boolean scanRutaPackageDAO(String rutaPackageDAO) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(rutaPackageDAO, false);
		if (ipackage != null) {
			return true;
		}
		return false;
	}

	public boolean existePackageDAO() {
		return (existePackageDAO && existePackageDAOBASE && existePackageDAOIMPL && existePackageDAOBaseDominioUtil && existePackageDAOBaseDominioUtilExceptionL);
	}

	public void createPackageAndClases() {
	}

	public void createPackageAndClasesDAOGeneral() throws JavaModelException {
		if (!existePackageDAO) {
			generarDAOPorEntidades(rutaPackageDAO, analizadorEntidadesDominio.getListaICompilationUnits());
		}
		if (!existePackageDAOBASE) {
			generarDAOBASEPorEntidades(rutaPackageDAOBASE, analizadorEntidadesDominio.getListaICompilationUnits());
		}
		if (!existePackageDAOIMPL) {
			generarDAOImplPorEntidades(rutaPackageDAOIMPL, analizadorEntidadesDominio.getListaICompilationUnits());
		}
		if (!existePackageDAOBaseDominioUtil) {
			generarDAOBASEDominioUtilPorEntidades(rutaPackageDAOBaseDominioUtil, analizadorEntidadesDominio.getListaICompilationUnits());
		}
		if (!existePackageDAOBaseDominioUtilExceptionL) {
			generarDAOBASEDominioUtilExceptionPorEntidades(rutaPackageDAOBaseDominioUtilException, analizadorEntidadesDominio.getListaICompilationUnits());
		}
	}


	private void generarDAOBASEDominioUtilExceptionPorEntidades(
			String rutaPackageDAOBaseDominioUtilException,
			List<ICompilationUnit> listaICompilationUnits) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(rutaPackageDAOBaseDominioUtilException, true);
		ICompilationUnit claseDAOBASE = null;
		String nameEntidad = "";
		for (ICompilationUnit iCompilationUnit : listaICompilationUnits) {
			nameEntidad = Util.getNameFile(iCompilationUnit.getElementName());
			claseDAOBASE = ipackage.createCompilationUnit(nameEntidad + "Exception" + EXTENSION_JAVA, getSourceGenereClaseDominioException(nameEntidad, rutaPackageDAOBaseDominioUtilException), false, null);
		}
	}

	private String getSourceGenereClaseDominioException(String nameEntidad,
			String rutaPackageDAOBaseDominioUtilException) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + rutaPackageDAOBaseDominioUtilException + ";");
		
		String strCabecera = "";
		{
			strCabecera += "\n	 public class " + nameEntidad + "Exception extends Exception  {\n";
		}
		String strMetodos = "\n				public "+nameEntidad+"Exception(String message) { " +
							"\n 				super(message); " +
							"\n 			} " +
							"\n 			public "+nameEntidad+"Exception(String message, Throwable throwable) { " +
							"\n 			    super(message, throwable); " +
							"\n 			} " +
							"\n 			public String getMessage() " +
							"\n 			{ " +
							"\n 			    return super.getMessage(); " +
							"\n 			}";		
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strMetodos);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	private void generarDAOBASEDominioUtilPorEntidades(
			String rutaPackageDAOBaseDominioUtil,
			List<ICompilationUnit> listaICompilationUnits) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(rutaPackageDAOBaseDominioUtil, true);
		ICompilationUnit claseDAOBASE = null;
		String nameEntidad = "";
		for (ICompilationUnit iCompilationUnit : listaICompilationUnits) {
			nameEntidad = Util.getNameFile(iCompilationUnit.getElementName());
			claseDAOBASE = ipackage.createCompilationUnit(nameEntidad + "Util" + EXTENSION_JAVA, getSourceGenereClaseDominioUtil(nameEntidad, rutaPackageDAOBaseDominioUtil,iCompilationUnit.getTypes()[0]), false, null);
		}
	}

	private String getSourceGenereClaseDominioUtil(String nameEntidad,
			String rutaPackageDAOBaseDominioUtil, IType iType) throws JavaModelException {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + rutaPackageDAOBaseDominioUtil + ";");		
		
		String strCabecera = "";
		{
			strCabecera += "\n	 		public class " + nameEntidad + "Util  {\n";
		}

		String strVariablesStaticas = "";		
		IField[] camposClase = iType.getFields();
		for (IField campo : camposClase) {
			strVariablesStaticas += "\n		public static String FIELD_"+campo.getElementName().toUpperCase()+"="+"\""+campo.getElementName()+"\";";
		}		
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strVariablesStaticas);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	private void generarDAOPorEntidades(String strRuta, List<ICompilationUnit> listaICompilationUnits) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(strRuta, true);
		ICompilationUnit interfazDAO = null;
		String nameEntidad = "";

		for (ICompilationUnit iCompilationUnit : listaICompilationUnits) {
			nameEntidad = Util.getNameFile(iCompilationUnit.getElementName());
			interfazDAO = ipackage.createCompilationUnit(nameEntidad + "DAO" + EXTENSION_JAVA, getSourceGenereInterfaz(nameEntidad, strRuta), false, null);

		}
	}

	private String getSourceGenereInterfaz(String nameEntidad, String strRuta) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + strRuta + ";");
		String strImports = "";
		{
			strImports += "\n " 
		+ "import java.util.List;" + "\n " 
					+ "import " + estructureObjectGeneralProject.getPackageBase() + DONT + CARPETA_DOMINIO + DONT + nameEntidad	+ ";" 
					+ "\n " 
					+ "import " + estructureObjectGeneralProject.getPackageBase() + DONT + CARPETA_UTIL + DONT + CLASS_IDAO + ";";
		}
		String strCabecera = "";
		{
			strCabecera = " public interface " + nameEntidad + "DAO extends IDAO<" + nameEntidad + "> {";
		}
		String strMetodos = "";
		{

		}
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strImports);
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strMetodos);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	private void generarDAOBASEPorEntidades(String strRuta, List<ICompilationUnit> listaICompilationUnits) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(strRuta, true);
		ICompilationUnit claseDAOBASE = null, claseDAOBASEBasic = null;
		String nameEntidad = "";
		ObjectCompilationUnit objectCompilationUnit = null;		
		
		claseDAOBASEBasic = ipackage.createCompilationUnit("DAOBase" + EXTENSION_JAVA, getSourceGenereDAOBase("DAOBase", strRuta), false, null);
		
		for (ICompilationUnit iCompilationUnit : listaICompilationUnits) {
			objectCompilationUnit = new ObjectCompilationUnit();
			nameEntidad = Util.getNameFile(iCompilationUnit.getElementName());
			objectCompilationUnit.setPaquete(strRuta);
			objectCompilationUnit.setEntidad(nameEntidad);
			claseDAOBASE = ipackage.createCompilationUnit(nameEntidad + "DAOBase" + EXTENSION_JAVA, getSourceGenereClase(nameEntidad, strRuta), false, null);
			objectCompilationUnit.setEntidadc(iCompilationUnit);
			objectCompilationUnit.setDaoBase(claseDAOBASE);
			listaObjectCompilationUnits.add(objectCompilationUnit);
		}
	}

	private String getSourceGenereDAOBase(String nameEntidad, String strRuta) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + strRuta + ";");
		String strImports = "";
		{
			strImports += "\n " 
					 + "import java.util.HashMap; \n "
					 + "import java.util.List; \n "
					 + "import java.util.Map; \n "
					 + "import java.util.Map.Entry; \n "
					 + "import javax.persistence.Query; \n " 
					 + "import javax.persistence.EntityManager; \n ";
		}
		String strCabecera = "";
		{
			strCabecera = " public class " + nameEntidad + " {\n";
		}
		String strCampos = "";
		{
			strCampos = "		private EntityManager em;\n";
		}
		String strMetodos = "";
		{
			strMetodos += "\n      @SuppressWarnings({  \"unchecked\", \"cast\" , \"rawtypes\"})" +
					      "\n      public " + nameEntidad + "(EntityManager em) { \n" 
						+ "              this.em = em;" 
						+ "\n      }\n";

			

			strMetodos += 	"\n  	public List getQuery(String sqlQuery , Map<String, Object> map) throws Exception {	"+	
							"\n				Query q = em.createQuery(sqlQuery);	"+
							"\n				for (Entry<String, Object> entry : map.entrySet()){	"+
							"\n   				System.out.println(entry.getKey() + \"/\" + entry.getValue());	"+
							"\n    				q.setParameter(entry.getKey(), entry.getValue());	"+
							"\n				}			"+
							"\n				return q.getResultList();	"+
							"\n		}		\n";
			

			strMetodos += 	"\n  	public List<Object[]> getQueryObjects(String sqlQuery , Map<String, Object> map) throws Exception {	"+	
							"\n				Query q = em.createQuery(sqlQuery);	"+
							"\n				for (Entry<String, Object> entry : map.entrySet()){	"+
							"\n   				System.out.println(entry.getKey() + \"/\" + entry.getValue());	"+
							"\n    				q.setParameter(entry.getKey(), entry.getValue());	"+
							"\n				}			"+
							"\n				return q.getResultList();	"+
							"\n		}		\n";
			
			strMetodos += 	"\n  	public Object getSingleQuery(String sqlQuery , Map<String, Object> map) throws Exception {	"+	
							"\n				Query q = em.createQuery(sqlQuery);	"+
							"\n				for (Entry<String, Object> entry : map.entrySet()){	"+
							"\n   				System.out.println(entry.getKey() + \"/\" + entry.getValue());	"+
							"\n    				q.setParameter(entry.getKey(), entry.getValue());	"+
							"\n				}			"+
							"\n				return q.getSingleResult();		"+
							"\n		}		\n";
			
			strMetodos += "\n 	  public static Map<String, Object> getNewMap(){ " +
						  "\n 			return new HashMap<String, Object>(); " +
						  "\n  	  }";
			
		}
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strImports);
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strCampos);
		buffer.append("\n");
		buffer.append(strMetodos);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	private String getSourceGenereClase(String nameEntidad, String strRuta) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + strRuta + ";");
		String strImports = "";
		{
			strImports += "\n " 
					 + "import java.util.HashMap; \n "
					 + "import java.util.List; \n "
					 + "import java.util.Map; \n "
					 + "import java.util.Map.Entry; \n "
					 + "import javax.persistence.Query; \n "
					 + "import java.util.Date; \n "					 
					 + "import "+ estructureObjectGeneralProject.getPackageBase()+ DONT+ CARPETA_DOMINIO+ DONT+ nameEntidad+ "; \n "
					 + "import "+ estructureObjectGeneralProject.getPackageBase()+ DONT+ PACK_DAO_BASE_DOMINIO_UTIL+ DONT+ nameEntidad+ "Util; \n "
					 + "import "+ estructureObjectGeneralProject.getPackageBase()+ DONT+ PACK_DAO_BASE_DOMINIO_UTIL_EXCEPTION+ DONT+ nameEntidad+ "Exception; \n "
					 + "import javax.persistence.EntityManager; \n ";
		}
		String strCabecera = "";
		{
			strCabecera = " public class " + nameEntidad + "DAOBase extends DAOBase{\n";
		}
		String strCampos = "";
		{
			strCampos += "\n	public static String STR_MENSAJE_SIN_REGISTROS_EXCEPTION = \"No hay registro.\";"+
					     "\n	public static String STR_MENSAJE_ERROR_EXCEPTION = \"Error en la conexion.\";"+
					     "\n	public static String SQL_ENTIDAD = \""+nameEntidad+"\";"+
					     "\n	public static String SQL_QUERY = \" select p from \"+SQL_ENTIDAD+\" p \";"+
					     "\n	public static String SQL_WHERE = \"where\";"+
					     "\n	public static String SQL_SUFIJO_ENTIDAD = \" p. \";"+
					     "\n	public static String SQL_MENOS = \" < \";"+
					     "\n	public static String SQL_MAS = \" > \";"+
					     "\n	public static String SQL_QUERY_CONSTRUCTOR_ID = \" select new "+nameEntidad+"(p.id) from "+nameEntidad+" p \";";
		}
		String strMetodos = "";
		{
			strMetodos += 	  "\n   public " + nameEntidad + "DAOBase(EntityManager em) { \n" 
							+ "        			super(em); \n" 
							+ "    }\n";		
		}
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strImports);
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strCampos);
		buffer.append("\n");
		buffer.append(strMetodos);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	private void generarDAOImplPorEntidades(String strRuta, List<ICompilationUnit> listaICompilationUnits) throws JavaModelException {
		IPackageFragment ipackage = analizador.generateIPackage(strRuta, true);
		ICompilationUnit claseDAOBASE = null;
		String nameEntidad = "";
		for (ICompilationUnit iCompilationUnit : listaICompilationUnits) {
			nameEntidad = Util.getNameFile(iCompilationUnit.getElementName());
			claseDAOBASE = ipackage.createCompilationUnit(nameEntidad + "DAOImpl" + EXTENSION_JAVA, getSourceGenereClaseDAOImpl(nameEntidad, strRuta), false, null);
		}

	}

	private String getSourceGenereClaseDAOImpl(String nameEntidad, String strRuta) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("package " + strRuta + ";");
		String strImports = "";
		{
			strImports += "\n " 
					+ "import org.slf4j.Logger; \n " 
					+ "\n " + "import org.slf4j.LoggerFactory; \n " 
					+ "\n " + "import org.springframework.stereotype.Repository; \n "
					+ "\n " + "import "+ rutaPackageDAO+ DONT+ nameEntidad+ "DAO; \n "
					+ "\n "	+ "import "+ rutaPackageDAOBASE+ DONT+ nameEntidad+ "DAOBase; \n "
					+ "\n " + "import "+ estructureObjectGeneralProject.getPackageBase()+ DONT+ CARPETA_DOMINIO+ DONT+ nameEntidad+ "; \n "
					+ "\n "	+ "import "+ estructureObjectGeneralProject.getPackageBase() + DONT + CARPETA_UTIL + DONT + CLASS_DAO + ";";
		}
		
		String strCabecera = "";
		{
			strCabecera += "\n @Repository(\"" + nameEntidad + "DAO\") \n"
						+ " public class " + nameEntidad + "DAOImpl  extends DAO<" + nameEntidad + "> implements " + nameEntidad+ "DAO {\n";
		}
		String strCampos = "";
		{
			strCabecera += "\n private static final Logger logger = LoggerFactory.getLogger(" + nameEntidad + "DAOImpl.class); \n" 
						+ "   private " + nameEntidad + "DAOBase "+ nameEntidad.toLowerCase() + "DAOBase; ";
		}
		String strMetodos = "";
		{
			strMetodos += "\n   public void setBasicGenericService() { \n" 
					   + nameEntidad.toLowerCase() + "DAOBase = new " + nameEntidad + "DAOBase(em); \n" 
					   + "   }\n";
			strMetodos += "   public " + nameEntidad + "DAOBase getBasicService() { \n" 
					   + "        return " + nameEntidad.toLowerCase() + "DAOBase; \n" 
					   + "   }\n";		
			
		}
		String strFin = "";
		{
			strFin += "} ";
		}
		buffer.append("\n");
		buffer.append(strImports);
		buffer.append("\n");
		buffer.append(strCabecera);
		buffer.append("\n");
		buffer.append(strCampos);
		buffer.append("\n");
		buffer.append(strMetodos);
		buffer.append("\n");
		buffer.append(strFin);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	public void updatePackageAndClasesDAOForEntidad() {
		//si falta alguno hay q actualizar
	}

	public void generarMetodosBasicos() throws Exception {
		ObjectCompilationUnit objComUnit = null;
		for (int i = 0; i < listaObjectCompilationUnits.size(); i++) {
			objComUnit = listaObjectCompilationUnits.get(i);
			IType clase = objComUnit.getClasesPrincipal(objComUnit.getDaoBase().getTypes());
			System.out.println("clase:"+clase.getElementName());
			String metodosGenerics = getSourceGenereMetodosPorEntidad(objComUnit);
			System.out.println(metodosGenerics);
			clase.createMethod(metodosGenerics, null, false, null);	
		}
	}

	private String getSourceGenereMetodosPorEntidad(
			ObjectCompilationUnit objComUnit) throws JavaModelException {
		System.out.println("getSourceGenereMetodosPorEntidad:");
		StringBuffer strb = new StringBuffer();
		
		IType clase = objComUnit.getClasesPrincipal(objComUnit.getEntidadc().getTypes());
		String strMetododsPorInterfaz = "" , strMetododsPorEntidad , strMetodoFiltradoGeneral = "";
		ITypeHierarchy typeHier= clase.newSupertypeHierarchy(null);
		IType[] superInterfaces= typeHier.getAllInterfaces();
		IType interfazEntidad = null;
		for (IType iType2 : superInterfaces) {
			if(iType2.getElementName().equals(CLASS_ENTIDAD)){
				interfazEntidad = iType2;
				strMetododsPorInterfaz = generarPorInterfazEntidad(clase,interfazEntidad);
				strb.append(strMetododsPorInterfaz);
			}
		}			
		strMetododsPorEntidad = generarPorEntidad(clase,interfazEntidad);
		strb.append(strMetododsPorEntidad);
		strMetodoFiltradoGeneral = generarMetodoGeneral(clase,interfazEntidad);
		strb.append(strMetodoFiltradoGeneral);
		return strb.toString();
	}

	private String generarMetodoGeneral(IType clase, IType interfazEntidad) {
		
		return "";
	}

	private String generarPorEntidad(IType clase, IType interfazEntidad) throws JavaModelException {
		System.out.println("generarPorEntidad:");
		String strnombreCampo = null ;
		StringBuffer buffer = new StringBuffer();
		IMethod[] metodos = interfazEntidad.getMethods();
		boolean repetido = false;
		IField[] listaCampos = clase.getFields();
		for (IField iField : listaCampos) {
			repetido = false;
			System.out.println(iField.getElementName());		
			for (IMethod iMethod : metodos) {
				System.out.println(iMethod.getElementName());	
				System.out.println(Util.getCamporDesdeMetodoGET(iMethod.getElementName()));
				if(iField.getElementName().equals(Util.getCamporDesdeMetodoGET(iMethod.getElementName()))){
					repetido = true;
					break;				
				}
			}
			if(!repetido){
				strnombreCampo = iField.getElementName();
				IField iFieldCampo = clase.getField(strnombreCampo);
				System.out.println("exists:"+iFieldCampo.exists());
				if(iFieldCampo.exists()){
					buffer.append(getSourceSimpleSelectForCampoMethod(clase,strnombreCampo,iFieldCampo));
				}	
			}			
		}
		return buffer.toString();
	}

	private String generarPorInterfazEntidad(IType clase,
			IType interfazEntidad) throws JavaModelException {
		System.out.println("generarPorInterfazEntidad:");
		String strnombreCampo = null ;
		StringBuffer buffer = new StringBuffer();
		for (IMethod metodos : interfazEntidad.getMethods()) {
			System.out.println(metodos.getElementName());
			strnombreCampo = Util.getCamporDesdeMetodoGET(metodos.getElementName());
			System.out.println(strnombreCampo);
			if(permitido(strnombreCampo)){
				IField iFieldCampo = clase.getField(strnombreCampo);
				System.out.println("exists:"+iFieldCampo.exists());
				if(iFieldCampo.exists()){
					buffer.append(getSourceSimpleSelectForCampoMethod(clase,strnombreCampo,iFieldCampo));
				}
			}
		}
		return buffer.toString();
	}

	private String getSourceSimpleSelectForCampoMethod(IType clase, String strnombreCampo, IField iFieldCampo) throws JavaModelException {	
		String tipoParametro = Util.getTipoparamtroInstanceOf(iFieldCampo.getTypeSignature());
		String whereClause = "";
		String pametrosWhere  = "";
		String nameMethodBy  = "By";
		String mapParameters = "";
		
		pametrosWhere = tipoParametro+" "+strnombreCampo;
		whereClause =" where p."+strnombreCampo+" = :"+strnombreCampo+" ";
		mapParameters = " map.put(\""+strnombreCampo+"\", "+strnombreCampo+"); ";

		if (tipoParametro.equals(SringProjectMainResource.TYPE_ATRIBUTTE_STRING)) {
			nameMethodBy="Like";
			pametrosWhere = tipoParametro+" "+strnombreCampo;
			whereClause =" where p."+strnombreCampo+" like :"+strnombreCampo+" ";
			mapParameters = " map.put(\""+strnombreCampo+"\",\"%\"+ "+strnombreCampo+"+\"%\"); ";
		}
		if (tipoParametro.equals(SringProjectMainResource.TYPE_ATRIBUTTE_Date)) {
			nameMethodBy="Between";
			pametrosWhere = tipoParametro+" "+strnombreCampo +"Inicio , " +tipoParametro+" "+strnombreCampo+"Fin";
			whereClause =" where p."+strnombreCampo+" between :"+strnombreCampo+"Inicio and "+strnombreCampo+"Fin ";
			mapParameters = " map.put(\""+strnombreCampo+"Inicio\", "+strnombreCampo+"Inicio);\n  ";
			mapParameters += " map.put(\""+strnombreCampo+"Fin\", "+strnombreCampo+"Fin); ";
		}
		
	    String str = "\n   public List<"+clase.getElementName()+"> getLista"+clase.getElementName()+nameMethodBy+ Util.capitalizarPrimerCaracter(strnombreCampo)+"("+pametrosWhere+", String campoOrder, String order) throws Exception{"+
					 "\n          String query = \" select p from "+clase.getElementName()+" p "+whereClause+" \" ;	" +
					 "\n          if( (campoOrder!=null) && (!campoOrder.equals(\"\")) && (campoOrder.length()>0) ) {" +					
					 "\n                query += \" order by \" + campoOrder + \" \"+ order ;    " +		 
					 "\n          }  " +
					 "\n          query += \" ; \";    " +
					 "\n          Map<String, Object> map = getNewMap();" +
					 "\n          " + mapParameters +					
					 "\n          try {" +
					 "\n          List<"+clase.getElementName()+"> p = getQuery( query ,map);" +
					 "\n 			if (p != null) {" +
					 "\n 				return p;" +
					 "\n 			} else {" +
					 "\n 				throw new "+clase.getElementName()+"Exception(STR_MENSAJE_SIN_REGISTROS_EXCEPTION);" +
					 "\n 			}" +
					 "\n 		  } catch (Exception e) {" +
					 "\n 			   throw new "+clase.getElementName()+"Exception(STR_MENSAJE_ERROR_EXCEPTION);" +
					 "\n 		  }	" +					 
				 	 "\n     }";
	    
	    str +=    "\n   public Integer count"+clase.getElementName()+nameMethodBy+ Util.capitalizarPrimerCaracter(strnombreCampo)+"("+pametrosWhere+") throws Exception{"+
				 "\n          String query = \" select count(p) from "+clase.getElementName()+" p "+whereClause+"; \" ;	" +
				 "\n          Map map = getNewMap();" +
				 "\n          " + mapParameters +
				 "\n          try {" +
				 "\n          	Integer p = (Integer) getSingleQuery(query, map);" +
				 "\n          	return p;" +
				 "\n          } catch (Exception e) {" +
				 "\n          	throw new "+clase.getElementName()+"Exception(STR_MENSAJE_ERROR_EXCEPTION);" +
				 "\n          }	" +
			 	 "\n     }";
	    
		return str;
	}
	
	private boolean permitido(String strnombreCampo) {
		if(strnombreCampo.equals("id")){
			return false;
		}
		return true;
	}

	

}
